ROOTCFLAGS = $(shell root-config --cflags)
ROOTLIBS = $(shell root-config --libs)
ROOTGLIBS = $(shell root-config --glibs)
ROOTLDFLAGS = $(shell root-config --ldflags)
CINT = rootcint

CXX = g++
CXXFLAGS = -Wall -O2 $(ROOTCFLAGS) -g
CXXLIBS = $(ROOTLIBS)
CXXLIBS += $(EVENTSO)
TARGETDIR = $(G4WORKDIR)/tmp/$(G4SYSTEM)/$(G4TARGET)
name := g4test
G4TARGET := $(name)

EVTHDR = TEvent.h
EVTSRC = $(EVTHDR:.h=.cxx)
EVTDICT = $(EVTHDR:.h=Dict.cxx)
EVTOBJ = $(EVTHDR:.h=.o)
EVTDICTOBJ = $(EVTDICT:.cxx=.o)

HITHDR = TTrackerHit.h
HITSRC = $(HITHDR:.h=.cxx)
HITDICT = $(HITHDR:.h=Dict.cxx)
HITOBJ = $(HITHDR:.h=.o)

EVTSO = $(TARGETDIR)/libTEvent.so
EVTSODEP = $(HITOBJ) $(EVTOBJ) $(EVTDICTOBJ)
TESTSRC = test.cc
TARGET = r
OBJ = test.o


all: $(EVTSO)

$(EVTSO): $(EVTSODEP)
	@echo "Making shared lib $(EVTSO)"
	@if test -d $(TARGETDIR); \
		then echo "Target directory $(TARGETDIR) existed";\
	 	else mkdir $(TARGETDIR); fi
	$(CXX) -shared -o $(EVTSO) $(LDFLAGS) $(ROOTLIBS) $(CXXFLAGS) $(EVTSODEP)

$(EVTOBJ): $(EVTHDR) $(EVTDICT)
	@echo "Making $(EVTOBJ)"
	$(CXX) $(CXXFLAGS) -c $(EVTSRC)
	$(CXX) $(CXXFLAGS) -c $(EVTDICT)

$(EVTDICT): $(EVTHDR) $(EVTSRC) Makefile
	@echo "Generate dictionary $(EVTDICT)"
	$(CINT) -f $(EVTDICT) -c -p  $(ROOTCFLAGS) $(EVTHDR) $(HITHDR)

$(HITOBJ): $(HITHDR) $(HITSRC) Makefile
	@echo "Generate dictionary $(HITDICT)"
	$(CINT) -f $(HITDICT) -c -p -f $(ROOTCFLAGS) $(HITHDR)
	$(CXX) $(CXXFLAGS) -c $(HITSRC)
	$(CXX) $(CXXFLAGS) -c $(HITDICT)

clean:    
	rm -f *Dict*
	rm -f *.o $(TARGET) *.root
	rm -f $(EVENTSO)
